package cn.zch.cmbchina.util.cmb;
import org.apache.commons.beanutils.PropertyUtilsBean;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.*;
/**
 * @Class: MapUtil.java
 * @Description:
 * @Author：caiqf
 * @Date�?014-10-11
 */
@SuppressWarnings("all")
public class MapUtil {
  private Map map = new HashMap();
  private Set keySet = map.keySet();
  /**
   * @param javabean
   * @param m
   */
  public static void mapToBean(Object javabean, Map<Object, Object> m) {
    Method[] methods = javabean.getClass().getDeclaredMethods();
    for (Method method : methods) {
      if (method.getName().startsWith("set")) {
        Class<?>[] params = method.getParameterTypes();
        String field = method.getName();
        field = field.substring(field.indexOf("set") + 3);
        field = field.toLowerCase().charAt(0) + field.substring(1);
        Object value = m.get(field.toString());
        try {
          if (value != null && !"".equals(value)) {
            String pa = params[0].getName().toString();
            if (pa.equals("java.util.Date")) {
              value = new Date(((Date) value).getTime());
            }
            else if (pa.equals("java.lang.String")) {
              value = new String(value.toString());
            }
            else if (pa.equals("java.lang.Integer") || pa.equals("int")) {
              value = new Integer(value.toString());
            }
            else if (pa.equals("java.lang.Long")) {
              value = new Long(value.toString());
            }
            else if (pa.equals("java.lang.Double")) {
              value = new Double(value.toString());
            }
            else if (pa.equals("java.lang.Float")) {
              value = new Float(value.toString());
            }
            else if (pa.equals("java.lang.Short")) {
              value = new Short(value.toString());
            }
            else if (pa.equals("java.lang.Byte")) {
              value = new Byte(value.toString());
            }
            else if (pa.equals("java.lang.Boolean")) {
              value = new Boolean(value.toString());
            }
            method.invoke(javabean, value);
          }
        }
        catch (Exception e) {
          e.printStackTrace();
        }
      }
    }
  }
  /**
   * beanToMap
   *
   * @param javabean
   * @param m
   */
  public static Map<Object, Object> beanToMap(Object javabean) {
    //遍历实体类属性值
    Map<Object, Object> m = new HashMap<>();
    try {
      PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
      PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(javabean);
      for (int i = 0; i < descriptors.length; i++) {
        String name = descriptors[i].getName();
        Object value = propertyUtilsBean.getNestedProperty(javabean, name);
        if (!"class".equals(name)) {
          // System.out.println("key  " + name + ":" + value)
          m.put(name, value);
        }
      }
    }
    catch (Exception e) {
      m = null;
      e.printStackTrace();
    }
    return m;
  }
  public Object get(String key) {
    return map.get(key);
  }
  public void put(String key, Object value) {
    map.put(key, value);
  }
  public void sort() {
    List list = new ArrayList(map.keySet());
    //		Collections.sort(list, new Comparator() {
    //			public int compare(Object a, Object b) {
    //				return a.toString().compareTo(b.toString());
    //			}
    //		});
    this.keySet = new TreeSet(list);
  }
  public Set keySet() {
    return this.keySet;
  }
}
